//
//  WeaponDatabase.m
//  MilitaryDemo
//
//  Created by qianfeng01 on 15-7-7.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "WeaponDatabase.h"

#import "FMDatabase.h"
#import "SecondMOdel.h"
#import "FirstModel.h"
@implementation WeaponDatabase
{
    FMDatabase *_database;
}
+(instancetype)sharedManager
{
    static WeaponDatabase *manger=nil;
    @synchronized (self){
        if (manger==nil) {
            manger=[[WeaponDatabase alloc]init];
        }
        return manger;
    }
}
-(id)init
{
    NSString *dbPath=[[NSBundle mainBundle]pathForResource:@"com_ikantech_military" ofType:@"db"];
    _database=[[FMDatabase alloc]initWithPath:dbPath];
    return self;
}

-(NSMutableArray *)selectAll
{
    if ([_database open]==NO) {
        return nil;
    }
    
    NSString *mainSql=@"select * from a02";
    FMResultSet *mainSet=[_database executeQuery:mainSql];
    NSMutableArray *array=[NSMutableArray array];
    while ([mainSet next]) {
        FirstModel *model=[[FirstModel alloc]init];
        model.id=[mainSet stringForColumn:@"_id"];
        model.title=[mainSet stringForColumn:@"a0201"];
        model.des=[mainSet stringForColumn:@"a0202"];
        [array addObject:model];
    }
    [_database close];
    return array;
}
-(NSMutableArray *)selectAllatDes:(NSString *)des
{
    
    if ([_database open]==NO) {
        return nil;
    }
    NSString *sql=@"select * from a03 where a0304=?";
    FMResultSet *set=[_database executeQuery:sql,des];
    NSMutableArray *array=[NSMutableArray array];
    while ([set next]) {
        FirstModel *model=[[FirstModel alloc]init];
        model.id=[set stringForColumn:@"_id"];
        model.pic=[set stringForColumn:@"a0302"];
        model.title=[set stringForColumn:@"a0301"];
        model.des=[set stringForColumn:@"a0304"];
        [array addObject:model];
    }
    [_database close];
    return array;
}
-(NSMutableArray *)selectAllatID:(NSString *)ID
{
    
    if ([_database open]==NO) {
        return nil;
    }
    NSString *sql=@"select * from a04 where a0401=?";
    FMResultSet *set=[_database executeQuery:sql,ID];
    NSMutableArray *array=[NSMutableArray array];
    while ([set next]) {
        SecondMOdel *model=[[SecondMOdel alloc]init];
        model.pic=[set stringForColumn:@"a0402"];
        model.des=[set stringForColumn:@"a0404"];
        model.general=[set stringForColumn:@"a0405"];
        model.data=[set stringForColumn:@"a0406"];
        model.equip=[set stringForColumn:@"a0407"];
        model.develop=[set stringForColumn:@"a0408"];
        model.set=[set stringForColumn:@"a0409"];
        model.belong=[set stringForColumn:@"a0410"];
        model.comment=[set stringForColumn:@"a0411"];
        [array addObject:model];
    }
    [_database close];
    return array;
}
@end
